﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZSZ.DALFactory;
using ZSZ.IDAL;
using ZSZ.Model;

namespace ZSZ.Service
{
    public partial class BaseService<T> where T : BaseEntity
    {
        public IBaseDal<T> CurrentDal { get; set; }
        public IDbSession DbSession { get; set; }


        #region 查询
        /// <summary>
        /// 获取所有没有软删除的数据
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> GetAll()
        {
            return CurrentDal.GetAll();
        }

        /// <summary>
        /// 获取总数据条数
        /// </summary>
        /// <returns></returns>
        public long GetTotalCount()
        {
            return CurrentDal.GetTotalCount();
        }

        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="startIndex"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public IQueryable<T> GetPagedData(int startIndex, int count)
        {
            return CurrentDal.GetPagedData(startIndex, count);
        }

        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="total"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IQueryable<T> GetPagedData(out long total, int pageIndex = 1, int pageSize = 10)
        {
            return CurrentDal.GetPagedData(GetAll(), out total, pageIndex, pageSize);
        }

        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="entities"></param>
        /// <param name="total"></param>
        /// <param name="pageIndex">默认第1页</param>
        /// <param name="pageSize">默认一页10条</param>
        /// <returns></returns>
        public IQueryable<T> GetPagedData(IQueryable<T> entities, out long total, int pageIndex = 1, int pageSize = 10)
        {
            return CurrentDal.GetPagedData(entities, out total, pageIndex, pageSize);
        }

        /// <summary>
        /// 查找id=id的数据，如果找不到返回null
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T GetById(long id)
        {
            return CurrentDal.GetById(id);
        }
        #endregion

        #region CUD

        public bool MarkDeleted(long id)
        {
            CurrentDal.MarkDeleted(id);
            return DbSession.SaveChanges() > 0;
        }

        public void BatchMarkDeleted(long id)
        {
            CurrentDal.MarkDeleted(id);
        }

        public bool MarkDeleted(T entity)
        {
            CurrentDal.MarkDeleted(entity);
            return DbSession.SaveChanges() > 0;
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool BatchMarkDeleted(List<long> ids)
        {
            foreach (long id in ids)
            {
                BatchMarkDeleted(id);
            }
            return DbSession.SaveChanges() > 0;
        }


        public bool Deleted(T entity)
        {
            CurrentDal.Deleted(entity);
            return DbSession.SaveChanges() > 0;
        }

        public void BatchDeleted(T entity)
        {
            CurrentDal.Deleted(entity);
        }


        public T Add(T entity)
        {
            entity.IsDeleted = false;
            entity.CreateDateTime = DateTime.Now;
            CurrentDal.Add(entity);
            DbSession.SaveChanges();
            return entity;
        }

        public bool Update(T entity)
        {
            CurrentDal.Update(entity);
            return DbSession.SaveChanges() > 0;
        }
        #endregion
    }
}
